//获取登录的用户id
function getUserId(username, password) {

    var json = {
        userid: "",
        username: username,
        password: password
    };
    console.log(json)
    var postdata = JSON.stringify(json);
    $.ajax({
        type: "POST",
        contentType: "application/json",
        url: "getUserId.do",
        dataType: "json",
        data: postdata,
        success: function(data) {
            if(data.userid == 0){
                //alert("用户名或密码错误!!!")
            }
            else{

                document.cookie="userid="+data.userid;
                //alert(data.userid);
               // alert(getCookie('userid'))
            }
            /*
            alert(
                "username : " + data.username + "\npassword : " + data.password
            );*/
        },
        error: function() {
            alert("error...");
        }
    });
}

//获取全部的货物信息
function getGoods(){
    var json = {
        goodId : [],
        goodName : [],
        goodPrice : [],
    };
    console.log(json)
    var postdata = JSON.stringify(json);
    $.ajax({
        type: "POST",
        contentType: "application/json",
        url: "getGoods.do",
        dataType: "json",
        data: postdata,
        success: function(data) {
            console.log(data);
            selectGoods(data);
            // alert("用户名或密码错误!!!")

        },
        error: function() {
            alert("error...");
        }
    });
}





//获取全部的用户信息
function getDetails(){
    var json = {
        userId : getCookie("userid"),
        userName : "",
        address : "",
        age : 0,
        sex : ""
    };
    console.log(json)
    var postdata = JSON.stringify(json);
    $.ajax({
        type: "POST",
        contentType: "application/json",
        url: "getDetails.do",
        dataType: "json",
        data: postdata,
        success: function(data) {
            console.log(data);
            selectDetails(data);
            // alert("用户名或密码错误!!!")

        },
        error: function() {
            alert("error...");
        }
    });

}


//修改用户信息
function updateDetails(){
    var json = {
        userId : document.getElementById("details_id").innerText,
        userName : document.getElementById("details_username").innerText,
        address : document.getElementById("details_address").value,
        age : document.getElementById("details_age").value,
        sex : document.getElementById("details_sex").value
    };
    console.log(json)
    var postdata = JSON.stringify(json);
    $.ajax({
        type: "POST",
        contentType: "application/json",
        url: "updateDetails.do",
        dataType: "json",
        data: postdata,
        success: function(data) {
            console.log(data);
            //selectDetails(data);
            // alert("用户名或密码错误!!!")

        },
        error: function() {
            alert("error...");
        }
    });



}

//查询订单
function getOrders(){
    var json = {
        userName :  [],
        orderId : [],
        address : [],
        goodName: []
    };
    console.log(json)
    var postdata = JSON.stringify(json);
    $.ajax({
        type: "POST",
        contentType: "application/json",
        url: "getOrders.do",
        dataType: "json",
        data: postdata,
        success: function(data) {
            console.log(data);
            selectOrders(data);
            // alert("用户名或密码错误!!!")

        },
        error: function() {
            alert("error...");
        }
    });
}

//插入一个新的订单
function insertOrder(){
    var json = {
        userName :  [document.getElementById('orders_username').value],
        address:[],
        goodName: [document.getElementById('orders_goodname').value]
    };
    console.log(json)
    var postdata = JSON.stringify(json);
    $.ajax({
        type: "POST",
        contentType: "application/json",
        url: "insertOrder.do",
        dataType: "json",
        data: postdata,
        success: function(data) {
            console.log(data);
            //selectOrders(data);
            // alert("用户名或密码错误!!!")


        },
        error: function() {
            alert("error...");
        }
    });
}

//显示结果中使用
function getResult(){
    var json = {
        ip :  [],
        sql:[],
        time: []
    };
    console.log(json)
    var postdata = JSON.stringify(json);
    $.ajax({
        type: "POST",
        contentType: "application/json",
        url: "getResult.do",
        dataType: "json",
        data: postdata,
        success: function(data) {
            console.log(data);
            //selectOrders(data);
            // alert("用户名或密码错误!!!")
            showResult(data);
        },
        error: function() {
            alert("error...");
        }
    });
}

//清除结果
function clearResult(){
    var json = {
        ip :  [],
        sql:[],
        time: []
    };
    console.log(json)
    var postdata = JSON.stringify(json);
    $.ajax({
        type: "POST",
        contentType: "application/json",
        url: "clearResult.do",
        dataType: "json",
        data: postdata,
        success: function(data) {
            console.log(data);
            //selectOrders(data);
            // alert("用户名或密码错误!!!")
            //showResult(data);
            var table = document.getElementById("table-showResult");

            var rowNum = table.rows.length;
            if (rowNum > 0) {
                for (i = 1; i < rowNum; i++) {
                    table.deleteRow(i);
                    rowNum = rowNum - 1;
                    i = i - 1;
                }
            }

        },
        error: function() {
            alert("error...");
        }
    });
}